Skip to content

Fix geofence expiration overflow#2245

Open
Cooad wants to merge 2 commits into
owntracks:masterfrom
Cooad:fix/geofence-expiration-overflow
Open

Fix geofence expiration overflow#2245
Cooad wants to merge 2 commits into
owntracks:masterfrom
Cooad:fix/geofence-expiration-overflow

Conversation

@Cooad
Copy link
Copy Markdown

@Cooad Cooad commented May 19, 2026

GMS geofence registration silently fails with ApiException: 10 on devices with significant uptime. GMS internally computes elapsedRealtime() + expirationDuration to derive an absolute expiration time. When expirationDuration is Long.MAX_VALUE (the NEVER_EXPIRE constant), this addition overflows the signed long, producing a negative value that GMS rejects as invalid. The failure was silent because neither success nor failure was logged.
Fix:

  • Add success/failure logging to GMSGeofencingClient so registration outcomes are visible in device logs
  • Cap expirationDuration to Long.MAX_VALUE - SystemClock.elapsedRealtime() to prevent overflow

this.expirationDuration?.run(builder::setExpirationDuration)
this.expirationDuration?.run {
builder.setExpirationDuration(
minOf(this, Long.MAX_VALUE - SystemClock.elapsedRealtime()))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to set the explicit GMS NEVER_EXPIRE value if this is Long.MAX_VALUE?

  if (this == org.owntracks.android.location.geofencing.Geofence.NEVER_EXPIRE)
             com.google.android.gms.location.Geofence.NEVER_EXPIRE  // -1L, GMS sentinel
         else
             this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants